package sing.core.util

import com.tencent.mmkv.MMKV
import sing.core.base.appContext
import sing.core.ext.util.loge

class MMkvUtil {

    companion object {
        var mmk: MMKV? = null
    }

    init {
        if (mmk == null) {
            MMKV.initialize(appContext)
            mmk = MMKV.defaultMMKV()
        }
    }

    var isFirst: Boolean
        get() = mmk!!.decodeBool("app_is_first", true)
        set(isFirst) {
            mmk!!.encode("app_is_first", isFirst)
        }
    var isTestUrl: Boolean
        get() = mmk!!.decodeBool("is_test_url", false)
        set(isFirst) {
            mmk!!.encode("is_test_url", isTestUrl)
        }

    var network: String
        get() = mmk!!.decodeString("user_network", "")
        set(userNetwork) {
            mmk!!.encode("user_network", userNetwork)
        }

    var token: String
        get() = mmk!!.decodeString("user_token", "")
        set(token) {
            mmk!!.encode("user_token", token)
        }

    var userId: String
        get() = mmk!!.decodeString("user_id", "")
        set(user_id) {
            mmk!!.encode("user_id", user_id)
        }

    var userPhone: String
        get() = mmk!!.decodeString("user_phone", "")
        set(user_phone) {
            "用户的手机号：$user_phone".loge()
            mmk!!.encode("user_phone", user_phone)
        }
    var channelId: Int
        get() = mmk!!.decodeInt("user_channel_id_${userPhone}", 0)
        set(channelId) {
            mmk!!.encode("user_channel_id_${userPhone}", channelId)
        }

    var ip: String
        get() = mmk!!.decodeString("user_ip", "")
        set(ip) {
            mmk!!.encode("user_ip", ip)
        }

    var oaid: String
        get() = mmk!!.decodeString("user_oaid", "")
        set(oaid) {
            mmk!!.encode("user_oaid", oaid)
        }
    var imei: String
        get() = mmk!!.decodeString("user_imei", "")
        set(oaid) {
            mmk!!.encode("user_imei", oaid)
        }

    fun clearUserInfo(){
        token = ""
        userId = ""
        userPhone = ""
    }

    // 是否已关注公众号
    var followGZH: Boolean
        get() = mmk!!.decodeBool("user_follow_gzh_${userPhone}", false)
        set(follow) {
            mmk!!.encode("user_follow_gzh_${userPhone}", follow)
        }

    // 选择过公众号
    var choosedGZH: Boolean
        get() = mmk!!.decodeBool("user_choose_gzh_${userPhone}", false)
        set(follow) {
            mmk!!.encode("user_choose_gzh_${userPhone}", follow)
        }
}























